import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.nio.file.Paths;
import java.sql.Types;
import java.util.Collections;

public class CodeGenerator {

    public static void main(String[] args) {
            FastAutoGenerator.create("jdbc:mysql://localhost:3306/fullstackdemo?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2b8&allowPublicKeyRetrieval=true", "root", "123456")
                    .globalConfig(builder -> {
                        builder.author("+Le") // 设置作者
                                .enableSwagger() // 开启 swagger 模式
                                .outputDir(Paths.get(System.getProperty("user.dir")) + "/FullStackDemo/src/main/java"); // 指定输出目录到FullStackDemo
                    })
                    .dataSourceConfig(builder ->
                            builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                                int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                                if (typeCode == Types.SMALLINT) {
                                    // 自定义类型转换
                                    return DbColumnType.INTEGER;
                                }
                                return typeRegistry.getColumnType(metaInfo);
                            })
                    )
                    .packageConfig(builder ->
                            builder.parent("com.cino") // 设置父包名
                                    .moduleName("fullstack") // 设置父包模块名
                                    .pathInfo(Collections.singletonMap(OutputFile.xml, Paths.get(System.getProperty("user.dir")) + "/FullStackDemo/src/main/resources/mapper")) // 设置mapperXml生成路径到FullStackDemo
                    )
                    .strategyConfig(builder ->
                            builder.addInclude("user") // 设置需要生成的表名
                                    .addTablePrefix("t_", "c_") // 设置过滤表前缀
                                    .entityBuilder()
                                    .enableLombok() // 启用Lombok
                                    .enableTableFieldAnnotation() // 启用字段注解
                                    .controllerBuilder()
                                    .enableRestStyle() // 启用REST风格
                                    .serviceBuilder()
                                    .formatServiceFileName("%sService") // 格式化service文件名
                                    .formatServiceImplFileName("%sServiceImpl") // 格式化serviceImpl文件名
                    )
                    .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                    .execute();
    }
}
